package com.easy.component.network;


import android.content.Context;

import com.easy.component.BuildConfig;
import com.easy.component.network.interceptor.LoggingInterceptor;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

/**
 * 描述: {描述}
 * 作者: jack-song
 * 创建日期: 2017/9/11 19:23
 * 修改日期: 2017/9/11
 * 版本: {版本}
 */
public class EasyNetWork {
    /**
     * 上下文
     */
    private Context context;
    /**
     * 全局url
     */
    private String baseUrl;
    /**
     * header
     */
    private Map<String, String> header;
    /**
     * 默认超时时间
     */
    private static final int DEFAULT_MILLISECONDS = 30 * 1000;
    /**
     * okhttp
     */
    private OkHttpClient.Builder okHttpClientBuilder;
    /**
     * retrofit
     */
    private Retrofit.Builder retrofitBuilder;

    private static final EasyNetWork ourInstance = new EasyNetWork();

    public static EasyNetWork getInstance() {
        return ourInstance;
    }

    private EasyNetWork() {
        okHttpClientBuilder = new OkHttpClient.Builder();
        okHttpClientBuilder.connectTimeout(DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        okHttpClientBuilder.readTimeout(DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        okHttpClientBuilder.writeTimeout(DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        retrofitBuilder = new Retrofit.Builder();
        isDebug();
    }

    public void isDebug() {
        if (BuildConfig.DEBUG) {
            okHttpClientBuilder.addInterceptor(new LoggingInterceptor());
        }
    }

    public EasyNetWork setContext(Context context) {
        this.context = context;
        return this;
    }

    public Context getContext() {
        return context;
    }

    /**
     * 全局读取超时时间
     */
    public EasyNetWork setReadTimeOut(long readTimeOut) {
        okHttpClientBuilder.readTimeout(readTimeOut, TimeUnit.MILLISECONDS);
        return this;
    }

    /**
     * 全局写入超时时间
     */
    public EasyNetWork setWriteTimeOut(long writeTimeout) {
        okHttpClientBuilder.writeTimeout(writeTimeout, TimeUnit.MILLISECONDS);
        return this;
    }

    /**
     * 全局连接超时时间
     */
    public EasyNetWork setConnectTimeout(long connectTimeout) {
        okHttpClientBuilder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS);
        return this;
    }

    /**
     * 设置全局url
     *
     * @param baseUrl
     */
    public EasyNetWork setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
        return this;
    }

    /**
     * 获取全局url
     *
     * @return
     */
    public String getBaseUrl() {
        return baseUrl;
    }

    /**
     * 设置header
     *
     * @param header
     */
    public EasyNetWork setHeader(Map<String, String> header) {
        this.header = header;
        return this;
    }

    /**
     * 获取header
     *
     * @return
     */
    public Map<String, String> getHeader() {
        if (null == header || header.isEmpty()) {
            Map<String, String> reflexHeader = new HashMap<>();
            try {
                Class<?> forClass = Class.forName("com.easy.EasyHeader");
                Constructor<?> constructor = forClass.getConstructor();
                Object obj = constructor.newInstance();
                Method method = forClass.getMethod("getHeader", Context.class);
                reflexHeader = (Map<String, String>) method.invoke(obj, context);
            } catch (Exception e) {

            }
            return reflexHeader;
        } else {
            return header;
        }
    }

    /**
     * 添加全局拦截器
     */
    public EasyNetWork addInterceptor(Interceptor interceptor) {
        okHttpClientBuilder.addInterceptor(interceptor);
        return this;
    }

    /**
     * 添加Log拦截器
     */
    public EasyNetWork addLogInterceptor(Interceptor interceptor) {
        okHttpClientBuilder.addInterceptor(interceptor);
        return this;
    }

    /**
     * 重新初始化OkHttpClient
     *
     * @return
     */
    public static OkHttpClient resetOkHttpClient() {
        return getInstance().okHttpClientBuilder.build();
    }

    /**
     * 重新初始化Retrofit
     *
     * @return
     */
    public static Retrofit resetRetrofit() {
        return getInstance().retrofitBuilder.build();
    }

    /**
     * 获取OkHttpClient
     *
     * @return
     */
    public static OkHttpClient.Builder getOkHttpClientBuilder() {
        return getInstance().okHttpClientBuilder;
    }

    /**
     * 获取Retrofit
     *
     * @return
     */
    public static Retrofit.Builder getRetrofitBuilder() {
        return getInstance().retrofitBuilder;
    }

}
